﻿using System;
using UnityEngine;

using System.Globalization;

namespace HXu.BaseTools
{
    /// <summary>
    /// ColorTools 颜色工具 <br />
    /// /************************************************* <br />
    /// -- 内容：
    /// <br />
    ///     1A、颜色转换为颜色代码 <br />
    ///     1B、颜色代码转换为颜色 <br />
    /// <br />
    /// -- 使用方法：
    /// <br />
    ///     1、引用命名空间，直接调用。<br />
    /// *************************************************/ <br />
    /// </summary>
public static class ColorTools
    {
        /// <summary>
        /// 用于匹配十六进制颜色代码的字符串
        /// </summary>
        public static string hexColorRegex = "^#?(?:[0-9a-fA-F]{3,4}){1,2}$";
       
        /// <summary>
        /// 颜色种类
        /// </summary>
        public enum ColorType
        {
            RRGGBB = 0,
            RRGGBBAA = 1
        }

        #region API

        /// <summary>
        /// 1A、颜色转换为颜色代码
        /// </summary>
        /// <param name="color">颜色</param>
        /// <param name="colorType">颜色种类</param>
        /// <returns>颜色代码</returns>
        public static string ColorToHex(this Color color, ColorType colorType = ColorType.RRGGBBAA)
        {
            Color32 color32 = color;

            switch (colorType)
            {
                case ColorType.RRGGBB:
                    return "#" + $"{(color32.r << 16) | (color32.g << 8) | color32.b:X6}";
                case ColorType.RRGGBBAA:
                    return "#" + $"{(color32.r << 24) | (color32.g << 16) | (color32.b << 8) | color32.a:X8}";
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// 1B、颜色代码转换为颜色
        /// </summary>
        /// <param name="hexStr">颜色代码</param>
        /// <param name="color">颜色</param>
        /// <returns>颜色格式是否正确</returns>
        public static bool HexToColor(this string hexStr, out Color color)
        {
            if (!hexStr.RegularMatch(hexColorRegex))
                throw new Exception($"this hexStr（{hexStr}）format is not match hexRegex！");

            int startIndex = hexStr.StartsWith("#") ? 1 : 0;
            int length = hexStr.Length - startIndex;

            switch (length)
            {
                case 6:
                    {
                        var b = byte.Parse(hexStr.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier);
                        var g = byte.Parse(hexStr.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier);
                        var r = byte.Parse(hexStr.Substring(startIndex + 0, 2), NumberStyles.AllowHexSpecifier);
                        color = new Color32(r, g, b, 255); // Default alpha is 255 (opaque)  
                        return true;
                    }
                case 8:
                    {
                        var a = byte.Parse(hexStr.Substring(startIndex + 6, 2), NumberStyles.AllowHexSpecifier);
                        var b = byte.Parse(hexStr.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier);
                        var g = byte.Parse(hexStr.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier);
                        var r = byte.Parse(hexStr.Substring(startIndex + 0, 2), NumberStyles.AllowHexSpecifier);
                        color = new Color32(r, g, b, a);
                        return true;
                    }
                default:
                    throw new Exception($"this hexStr（{hexStr}）format is not match hexRegex！");
            }
        }

        #endregion

    }
}